<?php

function t($str, $lang = 'zh-cn'){
	
	return $string;
	
}

function l($text, $path, $options = array()) {
	
	$options += array(
		'attributes' => array(),
		'html' => FALSE,
	);
	
	if($path == $_GET['q']){
		if (isset($options['attributes']['class'])) {
	      $options['attributes']['class'] .= ' active';
	    }
	    else {
	      $options['attributes']['class'] = 'active';
	    }
	}
	
	if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
		$options['attributes']['title'] = strip_tags($options['attributes']['title']);
	}
	
	return '<a href="' . pithy_url($path, $options) . '"' . pithy_attributes($options['attributes']) . '>' . ($options['html'] ? $text : check_plain($text)) . '</a>';
}

/**
 * Url 生成
 * 
 * @param unknown_type $path
 * @param unknown_type $options
 * @return string
 */
function pithy_url($path = NULL, $options = array()){
	$options += array(
		'fragment' => '',
		'query' => '',
		'absolute' => false,
		'alias' => false,
		//'prefix' => ''
	);
	
	if ($options['fragment']) {
		$options['fragment'] = '#'. $options['fragment'];
	}
	
	global $base_url, $base_path;
	static $script;
	
	if(!isset($script)){
		$script = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === FALSE) ? 'index.php' : '';
	}
	
	if(!isset($options['base_url'])){
		$options['base_url'] = $base_url;
	}
	
	// Preserve the original path before aliasing.
	$original_path = $path;
	
	if($path == '<front>'){
		$path = '';
	}
	
	if (function_exists('custom_url_rewrite_outbound')) {
    	// Modules may alter outbound links by reference.
		custom_url_rewrite_outbound($path, $options, $original_path);
	}
	
	$base = $options['absolute'] ? $options['base_url'] .'/' : $base_path;
	$path = pithy_urlencode($path);
	
	if(variable_get('clean_url', 0)){
		if($options['query']){
			return $base . $path . '?' . $options['query'] . $options['fragment'];
		}else{
			return $base . $path . $options['fragment'];
		}
	}else{
		//Whithout Clean URLS
		$variables = array();
		if (!empty($path)) {
			$variables[] = 'q='. $path;
	    }
	    
		if (!empty($options['query'])) {
	      $variables[] = $options['query'];
	    }
	    
		if ($query = join('&', $variables)) {
	      return $base . $script .'?'. $query . $options['fragment'];
	    }
	    else {
	      return $base . $options['fragment'];
	    }
	}
}

function check_plain($text){
	return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}

function pithy_urlencode($text){
	if(variable_get('clean_url', 0)){
		return str_replace(array('%2F', '%26', '%23', '//'),
                       array('/', '%2526', '%2523', '/%252F'),
                       rawurlencode($text));
	}else{
		return str_replace('%2F', '/', rawurlencode($text));
	}
}

function pithy_attributes($attributes = array()){

	if(is_array($attributes)){
		$t = '';
		foreach ($attributes as $key => $value){
			$t .= " $key=" . '"' . check_plain($value) . '"';
		}
		
		return $t;
	}
}

function pithy_goto($path = ''){
	$url = pithy_url($path);
	
	header("Location: $url" );
	exit();
}

/**
 * Since $_SERVER['REQUEST_URI'] is only available on Apache
 */
function request_uri(){
	if(isset($_SERVER['REQUEST_URI'])){
		$uri = $_SERVER['REQUEST_URI'];
	}else{
		if(isset($_SERVER['argv'])){
			$uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['argv'][0];
		}elseif (isset($_SERVER['QUERY_STRING'])) {
	      	$uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING'];
	    }
	    else {
      		$uri = $_SERVER['SCRIPT_NAME'];
	    }
	}
	
	return $uri;
}

function show_404($msg = ""){
	echo 'Page not found! Please check your url!';
	exit();
}

function load_database(){
	static $instance = null;
	
    if ($instance === null) {
		global $conf;

        $o = $conf['database'];

        $instance = DB::factory($o['dbdriver'], $o);
    }

    return $instance;
}